package com.Web.Servlet;

import com.Web.util.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/ajax2")
public class Ajax2Servlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Ajax2Servlet.doPost");
        String name = req.getParameter("name");
        Map<String,Object> map = new HashMap<>();
        if("tom".equalsIgnoreCase(name)) {
            map.put("exist",true);
            map.put("msg","此用户已存在，请更换一个");
        } else if("".equals(name)) {
            map.put("exist",true);
            map.put("msg","用户不能为空");
        } else {
            map.put("exist",false);
            map.put("msg","此用户可用");
        }
        JSONUtil.toJSON(resp,map);
//        resp.setContentType("text/html;charset=UTF-8");
//        ObjectMapper mapper = new ObjectMapper();
//        mapper.writeValue(resp.getWriter(),map);
    }
}
